package com.itheima.streamdemo;

import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamDemo4 {

    public static void main(String[] args) {
        List<Student> list = List.of(
                new Student("张三丰", 100),
                new Student("张无忌", 18),
                new Student("赵敏", 19),
                new Student("周芷若", 17),
                new Student("张强", 20),
                new Student("李华", 100)
        );

        // 输出年龄大于18岁的学生的姓名
        list.stream()
                .filter(student -> student.getAge() > 18)
                .map(Student::getName)
                .forEach(System.out::println);

        // 输出年龄大于18岁的学生的数量
        long count = list.stream()
                .filter(student -> student.getAge() > 18)
                .count();
        System.out.println("年龄大于18的学生数量为：" + count);

        // 输出年龄最大的学生
        Student maxStudent = list.stream()
                .max((s1, s2) -> s1.getAge() - s2.getAge())
                .get();
        System.out.println("年龄最大的学生为：" + maxStudent.getName());

        // 输出年龄最小的学生
        Student minStudent = list.stream()
                .min((s1, s2) -> s1.getAge() - s2.getAge())
                .get();
        System.out.println("年龄最小的学生为：" + minStudent.getName());

        // 收集到集合中
        List<Student> list1 = list.stream()
                .filter(student -> student.getAge() > 18)
                .toList();

        // 收集到数组中
        Student[] students = list.stream()
                .filter(student -> student.getAge() > 18)
                .toArray(Student[]::new);

        // 收集到map中
        Map<String, Integer> map =  list.stream()
                .filter(student -> student.getAge() > 18)
                .collect(Collectors.toMap(Student::getName, Student::getAge));
    }
}

@Setter
@Getter
class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}